// pages/my/my.js
const app = getApp();
var router = require("../../router.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login: 0,
    content: 1, //内容
    motto: 'Hello World',
    userInfo: {
      nickName: "",
      avatarUrl: ""
    },
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  // 底部
  index: function () {
    app.radTo('../index/index')
  },
  dianzan: function () {
    app.radTo('../list/list')
  },
  my: function () {
    app.radTo('../my/my')
  },
  positive: function () {
    app.radTo('../positive/positive')
  }, 
  personal:function(){
    app.navTo('../personal/personal')
  },
  collection: function () {
    app.navTo('../collection/collection')
  },
  diary: function () {
    wx.navigateTo({
      url: '../diary/diary',
    })   
  },
  onLoad: function(){
    app.isLogin();
    this.setData({
      userInfo: {
        nickName: wx.getStorageSync("nickname"),
        avatarUrl: wx.getStorageSync("head")
      },
    });
  },
   onShow: function () {
    let that = this;
    app.request({
      url: router.msg.audit,
      method: "GET",
      success: function (data) {
        that.setData({
          login: data.login,
          content: data.content,
        })
      }
    });
   },
  //分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      var that = this;
      // 来自页面内转发按钮    
      var id = res.target.dataset.id;
      var index = res.target.dataset.ctr;
      var title = that.data.list[index].name;
      return {
        title: title,
        path: '/pages/my/my',
        success: function (res) {
          app.request({
            url: router.msg.share,
            method: "POST",
            data: {
              id: id,
            },
            success: function (data) {
              if (data) {
                var list = that.data.list;
                var share = list[index].share;
                list[index].share = share + 1;
                that.setData({
                  list: list
                })
                wx.showToast({
                  title: '分享成功',
                  icon: 'success',
                  duration: 2000
                });
              } else {
                wx.showToast({
                  title: '分享失败请重试',
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          });
        },
        fail: function (res) {
          wx.showToast({
            title: '分享已取消',
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  },
})